import os
import sys
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *

class ShotList(QListWidget):
    shot_info_Signal=Signal(str)
    def __init__(self):
        super(ShotList, self).__init__()
        self.data={}
        self.setMinimumWidth(240)
        self.setMinimumHeight(500)
        self.itemClicked.connect(self.show_data)


    def set_data(self,data):
        self.clear()
        self.data=data
        for k,v in data.items():
            current_data={}
            # name=k+"|"+v[0]
            # current_data[k]=v
            item=QListWidgetItem()
            item.setBackground(QBrush(QColor('#FFDEAD')))
            item.setText(k)
            # item.setData(5,current_data)
            item.setTextAlignment(Qt.AlignCenter)
            self.addItem(item)

    def show_data(self,item):
        # data=item.data(5)
        ma_name=item.text()
        print('select ma_name',ma_name)
        self.shot_info_Signal.emit(ma_name)



class MayaBatchRender(QDialog):

    def __init__(self):
        super(MayaBatchRender, self).__init__()
        self.current_data={}  #当前所有ma信息 {'ma1':data,'ma2':data}  #data={'shot_name':xxx,'fps':xxx,'start':xx,'end':xxx}


        self.setWindowTitle('Maya Batch Render')
        self.big_label_font=QFont('',25,65)
        self.mid_label_font=QFont('',15,65)
        self.main_layout=QVBoxLayout()
        self.show_label=QLabel('Maya  Batch Render')
        self.show_label.setFont(self.big_label_font)
        self.show_label.setAlignment(Qt.AlignCenter)
        self.info_layout=QHBoxLayout()
        self.left_layout=QVBoxLayout()
        self.shot_list=ShotList()
        self.shot_list.shot_info_Signal.connect(self._shot_info)
        self.left_layout.addWidget(self.shot_list)
        self.right_layout=QVBoxLayout()
        self.right_layout.setAlignment(Qt.AlignTop)
        self.shot_info_layout=QHBoxLayout()
        self.shot_info_layout.setContentsMargins(0,15,0,10)
        self.shot_info_label=QLabel('Shot Infomation')
        self.shot_info_label.setAlignment(Qt.AlignCenter)
        self.shot_info_label.setFont(self.mid_label_font)
        self.shot_info_layout.addWidget(self.shot_info_label)
        self.shot_layout=QHBoxLayout()
        self.shot_layout.setContentsMargins(0, 15, 0, 10)
        self.shot_name_label=QLabel('镜头号:')
        self.shot_name=QLabel('')
        self.shot_layout.addWidget(self.shot_name_label)
        self.shot_layout.addWidget(self.shot_name)
        self.shot_fps_layout=QHBoxLayout()
        # self.shot_fps_layout.setSpacing(10)
        self.shot_fps_layout.setContentsMargins(0, 15, 0, 10)
        self.shot_fps_label=QLabel('帧数率:')
        self.shot_fps_edit=QLineEdit()
        self.shot_fps_edit.setMaximumWidth(80)
        self.shot_fps_blank=QLabel('               ')
        # self.shot_fps_edit.setContentsMargins(0,0,50,0)
        self.shot_fps_layout.addWidget(self.shot_fps_label)
        self.shot_fps_layout.addWidget(self.shot_fps_edit)
        self.shot_fps_layout.addWidget(self.shot_fps_blank)
        self.shot_frame_layout=QHBoxLayout()
        self.shot_frame_layout.setContentsMargins(0, 15, 0, 10)
        self.shot_frame_label=QLabel("帧  数:")
        self.start_frame_edit=QLineEdit()
        self.end_frame_edit=QLineEdit()
        self.shot_frame_layout.addWidget(self.shot_frame_label)
        self.shot_frame_layout.addWidget(self.start_frame_edit)
        self.shot_frame_layout.addWidget(self.end_frame_edit)
        self.confirm_layout=QHBoxLayout()
        self.confirm_layout.setContentsMargins(50,25,50,0)
        self.confirm_btn=QPushButton('确认')
        self.confirm_btn.clicked.connect(self._reset)
        self.confirm_btn.setStyleSheet("QPushButton{color:black}"
                                      "QPushButton:hover{color:red}"
                                      "QPushButton{background-color:#FFDEAD}"
                                      "QPushButton{border:2px}"
                                      "QPushButton{border-radius:3px}"
                                      "QPushButton{padding:1px 1px}")
        self.confirm_layout.addWidget(self.confirm_btn)
        self.maya_path_layout=QHBoxLayout()
        self.maya_path_layout.setContentsMargins(0,50,0,10)
        self.maya_path_label=QLabel('maya.exe:')
        self.maya_path_edit=QLineEdit()
        self.maya_path_edit.setText(r'C:\Program Files\Autodesk\Maya2018\bin\Render.exe')
        self.maya_path_btn=QPushButton('...')
        self.maya_path_btn.clicked.connect(self._choose_maya)
        self.maya_path_btn.setStyleSheet("QPushButton{color:black}"
                                      "QPushButton:hover{color:red}"
                                      "QPushButton{background-color:#FFDEAD}"
                                      "QPushButton{border:2px}"
                                      "QPushButton{border-radius:3px}"
                                      "QPushButton{padding:1px 1px}")
        self.maya_path_layout.addWidget(self.maya_path_label)
        self.maya_path_layout.addWidget(self.maya_path_edit)
        self.maya_path_layout.addWidget(self.maya_path_btn)
        self.render_layout=QVBoxLayout()
        self.render_layout.setContentsMargins(0, 105, 0, 10)
        self.render_btn=QPushButton('Render')
        self.render_btn.clicked.connect(self._do_render)
        self.render_btn.setStyleSheet("QPushButton{color:black}"
                                      "QPushButton:hover{color:red}"
                                      "QPushButton{background-color:#FFDEAD}"
                                      "QPushButton{border:2px}"
                                      "QPushButton{border-radius:3px}"
                                      "QPushButton{padding:1px 1px}")
        self.render_btn.setMinimumHeight(50)
        self.render_layout.addWidget(self.render_btn)
        self.right_layout.addLayout(self.shot_info_layout)
        self.right_layout.addLayout(self.shot_layout)
        self.right_layout.addLayout(self.shot_fps_layout)
        self.right_layout.addLayout(self.shot_frame_layout)
        self.right_layout.addLayout(self.confirm_layout)
        self.right_layout.addLayout(self.maya_path_layout)
        self.right_layout.addLayout(self.render_layout)
        self.info_layout.addLayout(self.left_layout)
        self.info_layout.addLayout(self.right_layout)

        self.main_layout.addWidget(self.show_label)
        self.main_layout.addLayout(self.info_layout)
        self.setLayout(self.main_layout)

        self.current_data={'ma1':{'ma_name':'ma1','fps':'25','start':'1','end':'105','path':'x'},
                           'ma2':{'ma_name':'ma2','fps':'25','start':'1','end':'101','path':'x'},
                           'ma3':{'ma_name':'ma3','fps':'25','start':'1','end':'11','path':'x'}}
        self.init_list(self.current_data)

    def init_list(self,data):
        # pass
        # if data:
            # for k,v in data.items():
            #     shot_name
        self.shot_list.set_data(data)

    def _reset(self):
        ma_name=self.shot_name.text()
        fps=self.shot_fps_edit.text()
        start=self.start_frame_edit.text()
        end=self.end_frame_edit.text()
        if ma_name:
            data={'ma_name':ma_name,'fps':fps,'start':start,'end':end}
            self.current_data[ma_name]=data
            print('current_data:',self.current_data)

    def _shot_info(self,ma):
        """
        slot func to get current selected ma infomation
        :param ma:
        :return:
        """
        data=self.current_data[ma]
        print('data:',data)
        print(type(data))
        self.shot_name.setText(ma)
        self.shot_fps_edit.setText(data['fps'])
        self.start_frame_edit.setText(data['start'])
        self.end_frame_edit.setText(data['end'])

    def _choose_maya(self):
        """
        选择maya路径
        :return:
        """
        fileName=QFileDialog.getOpenFileName(None,'选择maya exe','','maya exe file(*.exe)')
        if fileName:
            exe_path=fileName[0].replace('/','\\')
            self.maya_path_edit.setText(exe_path)

    def _do_render(self):
        """
        进行批量渲染
        :return:
        """



if __name__ == '__main__':
    app=QApplication(sys.argv)
    m=MayaBatchRender()
    m.show()
    sys.exit(app.exec_())
